package cn.bob.system.service.impl;

import cn.bob.org.domain.Employee;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.system.domain.Menu;
import cn.bob.system.mapper.MenuMapper;
import cn.bob.system.service.IMenuService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-10-24
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> getmeumTree() {
        //获取顶级菜单发送给前端
        List<Menu> menus = menuMapper.findAll();
        List<Menu> collmenus = new ArrayList<>();
        //获取到顶级
        Map<Long, Menu> all = menus.stream().collect(Collectors.toMap(Menu::getId, e -> e));
        for (Menu menu : menus) {
            if (menu.getParentId()==null){
                collmenus.add(menu);
            }else {
                //查询到map中的父级
                Menu fmenu = all.get(menu.getParentId());
                if (fmenu!=null){
                    fmenu.getChildren().add(menu);
                }
            }
        }
        return collmenus;
    }

    @Override
    public List<Menu> getParentMeum() {
        //获取顶级菜单发送给前端
        List<Menu> menus = menuMapper.getParentMeum();
        List<Menu> collect = menus.stream().filter(e -> e.getParentId() == null).collect(Collectors.toList());
        return collect;
    }



}
